package com.ykt.controller;

import com.jfinal.aop.Before;
import com.ykt.common.utils.page.PageData;
import com.ykt.common.utils.page.PageParam;
import com.ykt.model.UserCollect;
import com.ykt.service.UserCollectService;
import com.ykt.validator.CommonValidator;
import com.ykt.validator.FavoriteValidator;
import com.ykt.vo.ResultVo;

import java.util.Date;

/**
 * 用户收藏操作
 * @author wing
 * @version 1.0.0
 */
public class FavoriteController extends BaseController{

	private UserCollectService userCollectService = enhance(UserCollectService.class.getName(), UserCollectService.class);
	
	/**
	 * 用户收藏列表
	 */
    @Before({CommonValidator.UserIdValidator.class})
    public void find(){
    	Integer userId = getParaToInt("userId",-1);
    	
    	PageParam<Integer> param = super.toPageParam(userId);
        PageData<UserCollect> pageData = userCollectService.findByUserId(param);
    	
    	this.returnJson(ResultVo.success(pageData));
    }

    /**
     * 添加或删除
     */
    @Before({CommonValidator.UserIdValidator.class, FavoriteValidator.class})
    public void add(){
    	Integer userId = getParaToInt("userId",-1);
    	Integer shopId = getParaToInt("shopId",-1);

        UserCollect model = new UserCollect();
        model.set(UserCollect.user_id, userId);
        model.set(UserCollect.shop_id, shopId);
        model.set(UserCollect.created_time, new Date());

        UserCollect uc = userCollectService.find(userId, shopId);
        if(uc == null || uc.get(UserCollect.id) == null){
            userCollectService.save(model);
        }

        this.returnJson(ResultVo.success());
    }

    /**
     * 删除收藏
     */
    @Before({CommonValidator.UserIdValidator.class, FavoriteValidator.class})
    public void delete(){
        Integer userId = getParaToInt("userId",-1);
        Integer shopId = getParaToInt("shopId",-1);

        userCollectService.delete(userId, shopId);
        
        this.returnJson(ResultVo.success());
    }
}
